package com.example.shader;

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity implements GLSurfaceView.Renderer{
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        GLSurfaceView view = new GLSurfaceView(this);
//        setContentView(R.layout.activity_main);
        setContentView(view);
        view.setEGLContextClientVersion(2);
        view.setRenderer(this);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub
        mTri = new Triangle(); 
        
        Bitmap bitmap = null;
        InputStream is = null;
        is = getResources().openRawResource(R.drawable.bg);
        if (null == is) {
            finish();
        }
        bitmap = BitmapFactory.decodeStream(is);
        mSprite = new Sprite(bitmap);
        
        GLES20.glClearColor(1, 1, 0, 1);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub  
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        
        mTri.draw();
        mSprite.draw();
        
        Log.i(TAG, "onDrawFrame");
    }
    
    
    private final static String TAG = MainActivity.class.getSimpleName();
    private Triangle mTri;
    private Sprite mSprite;
}
